package com.hwl.ucenter.controller;


import com.hwl.commonutils.exception.GuliException;
import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.result.ResultCodeEnum;
import com.hwl.commonutils.utils.JwtInfo;
import com.hwl.commonutils.utils.JwtUtils;
import com.hwl.servicebase.dto.MemberDto;
import com.hwl.ucenter.pojo.vo.LoginVo;
import com.hwl.ucenter.pojo.vo.RegisterVo;
import com.hwl.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-07
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/api/ucenter")
public class ApiMemberController {

    @Resource
    private MemberService memberService;

    @ApiOperation("用户注册")
    @PostMapping("register")
    public RespBean register(@RequestBody RegisterVo registerVo) {
        if ((memberService.register(registerVo))) {
            return RespBean.ok().message("注册成功！");
        }
        return RespBean.error().message("注册失败！");
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public RespBean login(@RequestBody LoginVo loginVo) {
        return memberService.login(loginVo);
    }

    @ApiOperation("获取用户登录信息")
    @GetMapping("getInfo")
    public RespBean getInfo(HttpServletRequest request) {
        try {
            JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
            return RespBean.ok().data(jwtInfo);
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }

    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("inner/getMemberDtoById/{memberId}")
    public MemberDto getMemberDtoById(@PathVariable String memberId) {
        return memberService.getMemberDtoById(memberId);
    }

}

